The MesmerizerExorciser is a machine language psychedelic page flipping experience that begins life encoded as hex stored as ASCII encased in a 2-line Applesoft BASIC program, because otherwise it wasn't all that interesting. There are 3 levels of loading to metamorphose the BASIC larvae into a pupa that then converts the remainder into a graceful chrysalis. What follows is a description of that process.
The first line of the program demonstrates the "SHellam" method, an improvement on the S.H. Lam method* of putting machine code bytes into memory by poking a string containing the ASCII representation into the keyboard buffer and then calling a ROM routine to process that data. The S.H. Lam method requires the use of a loop and POKE statements, and in a two line program every character counts. So the SHellam method eliminates the need for a loop and a POKE by re-directing print output directly into the keyboard buffer. It does this by changing the base address of the screen line where text output is set to go, which is at locations 40/41 ($28.29). If the string to be printed is longer than the current width of the screen (usually 40) then we poke a value at least one greater than the string length into location 33 so that the ROM will not perform a carriage return in the middle of data being poked into memory via the PRINT. Once these vectors are set, we simply print the string we want "typed" into the keyboard buffer. After that, a CALL -144 will process the string and execute any other monitor directives, which in this case is the command to execute the code that was entered via the string.
The second stage loader is thereby stuffed into memory and executed. It lives at $C9 in memory, in zero page. It was put there to take advantage of a couple instructions that modify itself, and doing that in zero page saved a couple bytes. And every byte counts in a two line program, in this case more so, because every byte in the second stage loader uses up 2-3 characters in the BASIC program. Originating at $C9, it starts just after the short code in memory that BASIC uses to fetch the next token of a running program, and overwrites a bunch of data that's used for graphics, so it won't hurt BASIC while it runs, not that it ultimately really matters, since BASIC is long gone once the 2nd stage loader is in memory, but we want to at least try to be polite. It also incidentally avoids turning on BASIC program protection at $D6 by just luckily have a 02 store there, for anyone that knows their BASIC zero page utilization and was paying attention to that.
This second stage loader begins reading the hex data stored right after the colon after the CALL -144 statement in line 0. It converts each two-character hex byte represented in ASCII into an actual value that gets stuffed into memory starting at location $300. It handles the line break (between line 0 and 1) by checking what the Y-register value is once a zero byte is encountered in the data stream, which is the BASIC end-of-line marker. It will be a specific value on the first pass, so it'll modify the loader to start reading data at the beginning of the next BASIC line (skipping the four bytes with the next-line pointer and line number) and then continues reading the ASCII hex data from line 1 until it reaches the end of that line.
Note that line 1 beginning with a C0 was a carefully calculated fluke. The first character of that line would have to be a non-digit to avoid BASIC trying to incorporate ASCII hex data into the line number. Everything in the end just worked out to where that C0 could be placed at the beginning of the line, so that avoided the need to start the line with a colon, which would have taken up a character that was needed to represent one full final byte of hex data.
Once the second stage loader has assembled the MesmerizerExorciser machine code at $300, it jumps to it, and off you go.
Each text page is filled with one specific ASCII character (when it first starts, a blank on one page and an inverted blank on the other). The code then flips between each page with a programmable frequency to produce exotic visual effects.
- Use the left and right arrow keys to change the frequency of the page flipping
- Use the up and down arrows to change the fill character of each screen
- Press RETURN to toggle between page 0/1 for adjusting the fill character
- Press ESC to exit the program
Note: after exiting, the BASIC environment is pretty clobbered, so you'll want to reboot to normalize things before proceeding on to anything else. However, you can still CALL -151 to get into the monitor and look around. The second stage loader will still (mostly) be at $C9 (a couple addresses are changed upon return to the BASIC prompt), and the MesmerizerExorciser will be at $300.390.
* SHellam of course being a play on/combination of/homage to this author and S.H. Lam; the S.H Lam method is described here: http://nparker.llx.com/a2/shlam.html